import java.util.Random;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class project {
    ListNode head;
    public void Solution(ListNode head) {
        this.head=head;
    }

    public int getRandom() {
        if(head==null){
            return -1;
        }
        Random random=new Random();
        ListNode cur=head;
        int val=cur.val;
        cur=cur.next;
        int i=1;
        while(cur!=null){
            i++;
            if(random.nextInt(i)==0){
                val=cur.val;
            }
            cur=cur.next;
        }
        return val;
    }
}
